package com.shj.web.util;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * bean工具类
 */
public class BeanUtil {

    /**
     * 从容器中获取bean
     * @param clazz
     * @param request
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz, HttpServletRequest request){
        WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        return applicationContext.getBean(clazz);
    }

}
